package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_TRADE_WAR_PUBLISH_WANTED, desc = "跨服商战发布通缉")
public class CrossTradeWarPublishWantedCmd extends CrossUnionCommand {
    private int activityId;
    private long wantedUserId;
    private long wantedServerId;
    private int useItemId;
    private long userId;
    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CrossTradeWarPublishWantedToCrossReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserBaseData());
        wantedUserId = reqMsg.getWantedUserId();
        wantedServerId = reqMsg.getWantedServerId();
        useItemId = reqMsg.getUseItemId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfoIfNotLoadDb(activityId);
        if(null == activityInfo){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        //发到游戏服去
        CrossTradeWarProto.CrossTradeWarPublishWantedToCrossReqMsg.Builder msg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossReqMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setUserId(userId);
        msg.setUserBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.setWantedUserId(wantedUserId);
        msg.setWantedServerId(wantedServerId);
        msg.setUseItemId(useItemId);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PUBLISH_WANTED_TO_WANTED, msg);
        MessageHelper.sendPacket(wantedServerId, wantedUserId, pbMsg);
    }

    @Override
    public void sendData() {
        if(0 != ret) {
            CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setActivityId(activityId);
            respMsg.setUseItemId(useItemId);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PUBLISH_WANTED_RESP, respMsg);
            MessageHelper.sendPacket(userBaseInfo.getServerId(), userId, pbMsg);
        }
    }
}
